﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mmht.BT;

public partial class admin_xtgly_xxxg : System.Web.UI.Page
{
    AdminBT adm = new AdminBT();
    public int pagesize = 10;
    public int pages = 0;
    public int page = 1;
    public int prePage = 1;
    public int forPage = 1;
    public int dataTotal = 0;
    public string page_url;
    public bool bl = false;   

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["admin_id"] == null)
        {
            Response.Redirect("admin_logout.aspx");
            return;
        }
        if (!IsPostBack)
        {
            if (Request["page"] != null)
            {
                string p = Request["page"];
                page = int.Parse(p);
            }
            else
            {
                page = 1;
            }
            ViewState.Add("page", page);
            page_url = Request.Url.ToString().Split('?')[0];
            controlRepeater();
        }
    }


    private void controlRepeater()
    {
        if (ViewState["page"] != null)
        {
            page = (int)ViewState["page"];
        }
        DataTable tabel1 = adm.SelectAdmin();
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = tabel1.DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = pagesize;
        pages = pds.PageCount;
        dataTotal = tabel1.Rows.Count;
        pds.CurrentPageIndex = page - 1;
        page = page < 1 ? 1 : page;
        page = page > pages ? pages : page;
        prePage = page - 1;
        forPage = page + 1;
        pds.CurrentPageIndex = page - 1;

        this.repeater1.DataSource = pds;
        this.repeater1.DataBind();
    }

    protected void rpt_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        int id = Convert.ToInt32(e.CommandArgument);
       
        switch (e.CommandName)
        {
            case "del":
                //删除代码
                bl =adm.DelAdmin (id);
                if (bl)
                {
                    this.lbMsg.Text = "删除成功";
                    controlRepeater();
                }
                else 
                {
                    this.lbMsg.Text = "操作失败";
                }
                break;
            case "update":
                Repeater repeater = (Repeater)sender;
                //string tbName = ((TextBox)e.Item.FindControl("tbName")).Text;
                string tbPassword = ((TextBox)e.Item.FindControl("tbPassword")).Text.ToString().Trim();
                string tbRePassword = ((TextBox)e.Item.FindControl("tbRePassword")).Text.ToString().Trim();
                string tbEmail = ((TextBox)e.Item.FindControl("tbEmail")).Text.ToString().Trim();
                string tbTel = ((TextBox)e.Item.FindControl("tbTel")).Text.ToString().Trim();
                //修改代码
                bl = false;                
                if(tbPassword !=tbRePassword )
                {
                    this.lbMsg.Text = "操作失败，两次密码输入不一致";
                    break ;
                }
                if(tbPassword .Trim ().Length ==0)
                {
                   bl=adm.UpdateAdmin1(tbEmail,tbTel ,id );                    
                }
                else
                {
                    bl = adm.UpdateAdmin2(tbPassword ,tbEmail ,tbTel ,id);
                }
                if (bl)
                {
                    this.lbMsg.Text = "更新成功";
                    controlRepeater();
                }
                else 
                {
                    this.lbMsg.Text = "操作失败";
                }
                break;
            default:
                break;
        }
    }

    public string getAdminname(int adminId)
    {
        return adm.FindbyId(adminId);
    }
}
